import java.util.regex.Pattern;

public class db_statement_insert_into implements db_interface_statement {

	public String[] oTokens = new String[9];
	public Pattern[] oRegex = new Pattern[3];
	private db_util_parser oUtilParser = null;
	private db_parse_result_insert_into oRes;
	db_statement_insert_into() {
		oTokens[0] = "INSERT";
		oTokens[1] = "INTO";
		oTokens[2] = "(";
		oTokens[3] = ")";
		oTokens[4] = "VALUES";
		oTokens[5] = "(";
		oTokens[6] = ")";
		oTokens[7] = ",";
		oTokens[8] = ",";

		oRegex[0] = Pattern.compile("(-)?(\\d){1,9}");
		oRegex[1] = Pattern.compile("'([^']|'')*'");
		oRegex[2] = Pattern.compile("[a-zA-Z][0-9a-zA-Z]*");

		oUtilParser = new db_util_parser(oTokens);
	}
	public db_interface_parse_result parse(String sSQL) {
		
		oRes = new db_parse_result_insert_into();
		
		oUtilParser.restart(sSQL);
		int iRes = query();

		if (oUtilParser.sQuery.trim().length() > 0 && iRes == 0)
			iRes = -1;
		oRes.set_result(iRes);
		return oRes;
	}

	public int query() {


		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[0]))
			return -2;

		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[1]))
			return -1;
		
		if(table() == -1)
			return -1;

		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[2]))
			return -1;
		if(insertdefinition() == -1)
			return -1;

		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oTokens[3]))
			return -1;

		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[4]))
			return -1;

		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[5]))
			return -1;
		if(insertvalues() == -1)
			return -1;

		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[6]))
			return -1;
		return 0;
	}

	public int insertdefinition() {

		if(name() == -1)
			return -1;
		oRes.oCols.add(oUtilParser.sValue);
		if (!oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[7])){
			//Nothing
			return 0;
		} else {
			if(insertdefinition() == -1){
				return -1;
			}
		}
		return 0;
	}

	public int insertvalues() {

		if (number() == -1 & string() == -1 ) {
			//Parse failure
			return -1;
		}
		if (oUtilParser.matchReplaceNoSpace(oUtilParser.sQuery, oTokens[8])){
			if(insertvalues() == -1)
				return -1;
		}
		return 0;
	}

	public int table() {
		if(name() == -1)
			return -1;
		oRes.sTable = oUtilParser.sValue;
		return 0;
	}

	public int number() {
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[0]))
			return -1;
		oRes.oDataTypes.add(new Integer(db_util.DB_DATATYPE_INT));
		oRes.oVals.add(oUtilParser.sValue);
		return 0;
	}

	public int string() {
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[1]))
			return -1;
		oRes.oDataTypes.add(new Integer(db_util.DB_DATATYPE_VARCHAR));
		oRes.oVals.add(oUtilParser.sValue.substring(1, oUtilParser.sValue.length()-1).replace("''", "'"));
		return 0;
	}

	public int name() {
		if (!oUtilParser.matchReplace(oUtilParser.sQuery, oRegex[2]))
			return -1;
		return 0;
	}
}